<template>
  <common-layout>
    <div class="top">
      <div class="header">
        <img alt="logo" class="logo" src="@/assets/img/logo.png" />
        <span class="title">{{ systemName }}</span>
      </div>
      <div class="desc">以代码敲击梦想</div>
    </div>
    <div class="register">
      <a-form :form="form" @submit="handleSubmit">
        <a-form-item label="用户名">
          <a-input
            autocomplete="autocomplete"
            placeholder="请输入用户名"
            v-decorator="[
              'username',
              {
                rules: [{ required: true, message: '请输入用户名' }],
                validateTrigger: 'change',
              },
            ]"
          >
            <a-icon slot="prefix" type="user" />
          </a-input>
        </a-form-item>

        <a-form-item label="手机号码">
          <a-input
            placeholder="请输入手机号码"
            v-decorator="[
              'phone',
              {
                rules: [
                  { required: true, message: '请输入手机号码' },
                  {
                    pattern: /^1[3456789]\d{9}$/,
                    message: '手机号码格式不正确',
                  },
                ],
                validateTrigger: 'blur',
              },
            ]"
            style="width: 100%"
          >
            <a-icon slot="prefix" type="phone" />
          </a-input>
        </a-form-item>

        <a-form-item label="密码" has-feedback>
          <a-input
            placeholder="请输入密码"
            v-decorator="[
              'password',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入密码',
                  },
                  {
                    pattern: /^[a-zA-Z0-9]{4,16}$/g,
                    message: '密码必须为4-16位的字母数字组合',
                  },
                  {
                    validator: validateToNextPassword,
                  },
                ],
                validateTrigger: 'blur',
              },
            ]"
            type="password"
          >
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>
        <a-form-item label="确认密码" has-feedback>
          <a-input
            placeholder="确认密码"
            v-decorator="[
              'confirm',
              {
                rules: [
                  {
                    required: true,
                    message: '请确认密码',
                  },
                  {
                    validator: compareToFirstPassword,
                  },
                ],
                validateTrigger: 'blur',
              },
            ]"
            type="password"
            @blur="handleConfirmBlur"
          >
            <a-icon slot="prefix" type="lock" />
          </a-input>
        </a-form-item>

        <a-form-item id="validator">
          <a-col :span="11">
            <a-input
              v-decorator="[
                'verificateCode',
                {
                  rules: [
                    { required: true, message: '请输入验证码' },
                    { validator: validate },
                  ],
                  validateTrigger: 'blur',
                  validateFirst: true,
                },
              ]"
              @pressEnter="handleSubmit"
              size="large"
              placeholder="验证码"
            >
              <a-icon slot="prefix" type="safety" />
            </a-input>
          </a-col>
          <a-col :span="11" :offset="2">
            <validate-code
              ref="validate-code"
              @change="(code) => (validateCode = code)"
              style="marginLeft:1rem"
            ></validate-code>
          </a-col>
        </a-form-item>

        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            :loading="logging"
            size="large"
            style="width:100%"
          >
            注册
          </a-button>
        </a-form-item>
        <a-form-item>
          <a-button
            type="primary"
            size="large"
            style="width:100%"
            @click="() => this.$router.push('/login')"
          >
            返回登录
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </common-layout>
</template>

<script>
import CommonLayout from '@/layouts/CommonLayout';
import ValidateCode from '@/pages/components/ValidateCode';
import { register } from '@/services/user';

export default {
  name: 'register',
  components: { CommonLayout, ValidateCode },
  beforeCreate() {
    this.form = this.$form.createForm(this);
  },
  data() {
    return {
      logging: false,
      validateCode: '',
      error: '',
      name: '',
      phone: '',
      password: '',
    };
  },
  methods: {
    //注册
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          this.loading = true;
          const name = values.username;
          const phone = values.phone;
          const password = values.password;
          const type = 2;
          register(name, phone, password, type)
            .then((res) => {
              this.loading = false;
              this.$message.success(res.msg);
              this.$router.push('/login');
            })
            .catch((err) => {
              this.loading = false;
              this.$message.error(err.msg);
            });
        }
      });
    },
    //密码校验
    handleConfirmBlur(e) {
      const value = e.target.value;
      this.confirmDirty = this.confirmDirty || !!value;
    },
    //确认密码校验
    compareToFirstPassword(rule, value, callback) {
      const form = this.form;
      if (value && value !== form.getFieldValue('password')) {
        callback('两次输入的密码不一致!');
      } else {
        callback();
      }
    },
    validateToNextPassword(rule, value, callback) {
      const form = this.form;
      if (value && this.confirmDirty) {
        form.validateFields(['confirm'], { force: true });
      }
      callback();
    },
    //验证码校验
    validate(rule, value, callback) {
      if (value.toUpperCase() !== this.validateCode) {
        callback(new Error('验证码输入错误，请重新输入'));
      }
      callback();
    },
  },
  computed: {
    systemName() {
      return this.$store.state.setting.systemName;
    },
  },
};
</script>

<style lang="scss" scoped>
.common-layout {
  .top {
    text-align: center;
    .header {
      height: 44px;
      line-height: 44px;
      a {
        text-decoration: none;
      }
      .logo {
        height: 44px;
        vertical-align: top;
        margin-right: 16px;
      }
      .title {
        font-size: 33px;
        color: rgba(0, 0, 0, 0.85);
        font-family: 'Myriad Pro', 'Helvetica Neue', Arial, Helvetica,
          sans-serif;
        font-weight: 600;
        position: relative;
        top: 2px;
      }
    }
    .desc {
      font-size: 14px;
      color: rgba(0, 0, 0, 0.45);
      margin: 12px auto 40px;
    }
  }
  .register {
    width: 380px;
    margin: 0 auto;
    @media screen and (max-width: 576px) {
      width: 95%;
    }
    @media screen and (max-width: 320px) {
      .captcha-button {
        font-size: 14px;
      }
    }
    .icon {
      font-size: 24px;
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;
    }
    #validator {
      margin-top: 20px !important;
    }
  }
  .ant-form-item {
    margin-bottom: 8px !important;
  }
}
</style>
